
import variables
from profile import profile
from post import postReadings

import time
import threading
import os


import PIR
import interfacei2clightsensor as Light
import ds1820_final_1 as Temp


profile.profileInit()
profile.printProfile()

PostTask = threading.Thread(target = postReadings, args=())

PostTask.start()

# Light.APDS_init()

prevTime = time.time()
nowTime = time.time()

logTime = time.time()

while True:
#     print('alarmCount: ', variables.alarmCount)
        nowTime = time.time()

	if nowTime-logTime > 3600:
                variables.logFile.close()
                logTime = nowTime
                variables.logFile = open(os.getcwd() + '/logs/' + str(time.asctime().replace(' ', '_')), 'w')
	
	if nowTime-prevTime > 1:
		prevTime = nowTime
		
		if variables.alarmCount == 0:
                        variables.lockTimestamp.acquire()
			variables.timestamp = nowTime
			variables.lockTimestamp.release()
		    
		if variables.alarmCount % profile.SamplingPeriod == 0:
                        variables.alarmRead = 1
			variables.alarmRead = 0
			variables.logFile.write('\nSampling - ' + time.asctime())
			variables.logFile.flush()
			
			'''
			if variables.alarmCount%2 == 0:
				variables.TempReadings += (0.0,)
				variables.LightReadings += (0.0,)
				variables.PirReadings += (0,)
				
		    	else:
				variables.TempReadings += (1.0,)
				variables.LightReadings += (1.0,)
				variables.PirReadings += (1,)		   

			'''

			variables.lockTemp.acquire()	
			variables.TempReadings += (Temp.read_temp(),)
			variables.lockTemp.release()

                        variables.lockLight.acquire()
			Light.APDS_init()
			variables.LightReadings += (Light.APDS_read(),)
			variables.lockLight.release()

                        variables.lockPir.acquire()
			variables.PirReadings += (PIR.PIR_Read(),)
                        variables.lockPir.release()
		
##			print(variables.TempReadings)
##			print(variables.LightReadings)
##			print(variables.PirReadings)
		    
		variables.alarmCount += 1
		if variables.alarmCount % profile.PublishPeriod == 0:
		    variables.alarmUpload = 1
		    variables.alarmCount = 0
		    time.sleep(0.005)
		
		#time.sleep(1)

